import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

import '../common/const/app_style_const.dart';
import '../common/type/bottom_action_sheet_type.dart';

class BottomActionSheet extends StatelessWidget {
  final bool cancelButtonVisible;
  final VoidCallback? onCancelPressed;
  final List<Widget>? actionWidgetList;
  final String label;
  final List<BottomActionSheetItemType>? actionList;

  const BottomActionSheet(
      {Key? key,
      this.label = "请选择您的操作",
      this.cancelButtonVisible = true,
      this.onCancelPressed,
      this.actionList,
      this.actionWidgetList})
      : super(key: key);

  List<Widget>? buildActions() {
    if (null != actionList) {
      List<Widget> list = [];
      for (var item in actionList!) {
        list.add(CupertinoActionSheetAction(
          onPressed: item.onPressed,
          child: Text(
            item.label,
            style: bottomActionSheetActionStyle,
          ),
        ));
      }

      return list;
    }

    if (null != actionWidgetList) {
      return actionWidgetList;
    }

    return [];
  }

  @override
  Widget build(BuildContext context) {
    return CupertinoActionSheet(
      title: Text(
        label,
        style: bottomActionSheetTitleStyle,
      ),
      actions: buildActions(),
      cancelButton: cancelButtonVisible
          ? CupertinoActionSheetAction(
              onPressed: onCancelPressed ??
                  () {
                    Navigator.of(context).pop();
                  },
              child: const Text(
                '取消',
                style: bottomActionSheetCancelBtnStyle,
              ),
            )
          : null,
    );
  }
}
